(function() {

	nsbase.views.index = new IndexFunc();

	function IndexFunc() {
		var _self = this;

		_self.init = init;

		var utilities = nsbase.utility,
        	clientResource = nsbase.resources.clientResource;
		var cssClass = {
			failed:"failed"
		}
		
		function init() {
			$(".nav_menu li.Index").addClass("selected");
			// banner roll
			$("#bannerContainer").orbit({
				loadingImage: "../static/images/icon/loading.gif",
				directionalNav: false,
				bullets: false
			});

			// pic wall mask effect
			$("#wallContainer").find("li").hover(function() {
				var $grayLayer = $(this).find('.gray_layer');
				$grayLayer.fadeIn(200,function(){
					var $graySpan = $grayLayer.find('span');
					var marTop = ($grayLayer.height() - $graySpan.height())/2;
					$graySpan.css("top",marTop);
					$graySpan.show();
				});

			}, function() {
				$(this).find(".gray_layer").fadeOut(200);
			});

			$("#wechat").tooltip({
				content:function () {
					return '<img src="../static/images/qrcode.png" width="200px" height="200px" alt=""/>';
				}
			});

			bindRechargeFrom();
		}

		function bindRechargeFrom () {
			var $txtEmail = $("#txtEmail"),
				$txtName = $("#txtName"),
				$txtContent = $("#txtContent"),
				$btnSend = $("#btnSend"),
				$feedbackForm = $(".cont_feedback");

			$txtEmail.focus(inputFocus).blur(function(){ inputEmptyValidate([$(this)]);});
			$txtName.focus(inputFocus).blur(function(){ inputEmptyValidate([$(this)]);});
			$txtContent.focus(inputFocus).blur(function(){ inputEmptyValidate([$(this)]);});

			$txtEmail.blur(function () {
				var sender = $(this);
				var defaultValue = sender.attr("default-value");
				var value = utilities.trim(sender.val());
				if(value == defaultValue){
					return;
				}
				sender.tooltip();
				if (!sender.hasClass(cssClass.failed) && !utilities.validations.emailValidate(value)) {
					sender.addClass(cssClass.failed);
					sender.attr("title",clientResource.msgInvalidEmail);
				} else {
					sender.removeClass(cssClass.failed).removeAttr("title");
					sender.tooltip("destroy");
				}
			});

			$btnSend.click(function(){
				var $feedbackForm = $(".cont_feedback");
				inputEmptyValidate([$txtEmail,$txtContent,$txtName]);
				if($feedbackForm.find("."+cssClass.failed).length > 0){
					return;
				}
				$btnSend.mask();
				var param = {
					email:utilities.trim($txtEmail.val()),
					name:utilities.trim($txtName.val()),
					content:utilities.trim($txtContent.val())
				};
				nsbase.ajax.request(nsbase.config.actionUrl.index.feedback, param, function(result) {
					$btnSend.unmask();
					if(result.success){
						utilities.errorDialog(clientResource.msgFeedbackSupport);
					} else {
						utilities.errorDialog(result.message);
					}
				},function(){
					$btnSend.unmask();
				});

			});

			function inputFocus () {
				var sender = $(this);
				var defaultValue = sender.attr("default-value");
				var value = utilities.trim(sender.val());
				
				if(value == defaultValue){
					sender.val("");
				}
			}

			function inputEmptyValidate(inputs){
				$.each(inputs,function(index,inputObj){
					var defaultValue = inputObj.attr("default-value");
					var value = utilities.trim(inputObj.val());
					inputObj.tooltip();
					if(!value || value == defaultValue){
						inputObj.val(defaultValue);
						inputObj.addClass(cssClass.failed);
						inputObj.attr("title",utilities.stringFormat(clientResource.msgNoEmpty,""));
					} else {
						inputObj.removeClass(cssClass.failed).removeAttr("title");
						inputObj.tooltip("destroy");
					}
				});
			}
		}

	}
})();